package com.ws.service;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ws.mapper.AdminsMapper;
import com.ws.domain.Admins;
import com.ws.util.SafeUtil;

import java.util.List;

@Service
public class AdminsService {
    @Autowired
    private AdminsMapper adminsMapper;

    public void setAdminsDao(AdminsMapper adminsMapper) {
        this.adminsMapper = adminsMapper;
    }

    public Admins login(String username, String password) {
        //登录查询时，需要先给密码加密
        System.out.println("---------------------");
        System.out.println(adminsMapper);
        return adminsMapper.login(username, SafeUtil.encode(password));
    }
    /**
     * 分页查询管理员
     * @param page 当前页
     * @param size 页大小
     * @return
     */
    public List<Admins> pageAdmin(int page,int size){
        PageHelper.startPage(page,size);
        return adminsMapper.findAll();
    }

    /**
     * 添加管理员
     * @param admin
     */
    public void adminSave(Admins admin) {
        //添加管理员前，把管理员密码加密
        admin.setPassword(SafeUtil.encode(admin.getPassword()));
        adminsMapper.adminSave(admin);
    }

    /**
     * 重置管理员密码
     * @param admin
     */
    public void adminReset(Admins admin) {
        //重置管理员密码前，把管理员密码加密
        admin.setPassword(SafeUtil.encode(admin.getPassword()));
        adminsMapper.adminReset(admin);
    }

    /**
     * 删除管理员
     * @param id
     */
    public void adminDelete(int id) {
        adminsMapper.adminDelete(id);
    }

}